package category_eng.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class category implements EntryPoint {
	DecoratorPanel categoryPanel = new DecoratorPanel();
	ProductList pl;
	public void onModuleLoad() {
		getCategoryArray();
		categoryPanel.setStyleName("categoryPanel");
		pl = getProductList();
		RootPanel.get("category").add(categoryPanel);
		RootPanel.get("productlist").add(pl);
	}

	private ProductList getProductList(){
		Element searchBox = Document.get().getElementById("search");
		if(searchBox==null)return new ProductList("?category=0&lang=eng");
		else {
			String productIds = searchBox.getInnerText();
			return new ProductList("?search="+productIds);
			}
	}
	
	  private void getCategoryArray(){
		  String url = GWT.getModuleBaseURL() + URL.encode("getCategoryService?lang=eng");
		  RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		  try {
			builder.sendRequest(null, new GetContentRequestCallback());
		  } catch (RequestException e) {
		  }
	  }

	  private void createTree(Tree tree, TreeItem staticTree,ArrayList<Anchor> elements) {
		    for(Anchor a:elements){
		    	staticTree.addItem(a);
		    }
		    staticTree.ensureDebugId("static-tree");
		    tree.addItem(staticTree);
		  }

	  private class GetContentRequestCallback implements RequestCallback {
			public GetContentRequestCallback() {
				super();
			}
			public void onError(Request request, Throwable exception) {
			}
			public void onResponseReceived(Request request, Response response) {
			   if (200 == response.getStatusCode()) {
				   	  categoryPanel.clear();
			    	  String respText = response.getText();
			    	  if(respText.equals(""))return;
			    	  JSONValue respValue = JSONParser.parseLenient(respText);
			    	  JSONArray receivedArr = respValue.isArray();
			    	  ScrollPanel staticTreeWrapper = new ScrollPanel();
			  	      staticTreeWrapper.setHeight("100%");
			  	      final Tree catTree = new Tree();
					  for(int i=0;i<receivedArr.size();i++){
							JSONObject jo = receivedArr.get(i).isObject();
							String catName = jo.get("CatName").isString().stringValue();
							JSONArray catArray = jo.get("CatArray").isArray();
							 final ArrayList<Anchor> a0 = new ArrayList<Anchor>();
							 String groupId = null;
							 Anchor groupAnchor = null;
							 String groupQuery = null;
							 for(int ii=0;ii<catArray.size();ii++){
								   JSONObject element = catArray.get(ii).isObject();
									double tempd = element.get("CatID").isNumber().doubleValue();
									final String id = new Integer((int)tempd).toString();
									final String value = element.get("SubCategory").isString().stringValue();
									final String query = "<span style='font-size:12;'>"+value+"</span>";
									final Anchor anchor = new Anchor();
									anchor.setHTML(query);
									anchor.addClickHandler(new ClickHandler(){
									public void onClick(ClickEvent event) {
										formatAllCatAnchor(catTree);
										anchor.setHTML("<font color='red'>"+query+"</font>");
										pl.query = "?category="+id+"&lang=eng";
										pl.getProductsMap();
									}});
									if(groupId == null)groupId = id;
									if(groupAnchor == null)groupAnchor= anchor;
									if(groupQuery == null)groupQuery = query;
								 a0.add(anchor);
							 }
							 Anchor catAnchor = new Anchor();
							 catAnchor.setHTML("<span style='font-size:12;'>"+catName+"</span>");
							 final TreeItem staticTree = new TreeItem(catAnchor);
							 final String gid=groupId;
							 final Anchor ganchor = groupAnchor;
							 final String gquery = groupQuery;
							 catAnchor.addClickHandler(new ClickHandler(){
								public void onClick(ClickEvent event) {
									formatAllCatAnchor(catTree);
									staticTree.setState(true);
									ganchor.setHTML("<font color='red'>"+gquery+"</font>");
									pl.query = "?category="+gid+"&lang=eng";
									pl.getProductsMap();
								}
							 });
							 createTree(catTree,staticTree,a0);
						}
					  staticTreeWrapper.add(catTree);
					  categoryPanel.add(staticTreeWrapper);
		      } 
			}
		}
	  private void formatAllCatAnchor(Tree ds){
		  int widgetCount = ds.getItemCount();
		  for(int i=0;i<widgetCount;i++){
			  TreeItem sp = (TreeItem)ds.getItem(i);
			  Anchor anchor = (Anchor)sp.getWidget();
			  anchor.setHTML("<span style='font-size:12;'>"+anchor.getText()+"</span>");
			  int childCount = sp.getChildCount();
			  for(int j=0;j<childCount;j++){
				  Anchor a = (Anchor)sp.getChild(j).getWidget();
				  a.setHTML("<span style='font-size:12;'>"+a.getText()+"</span>");
			  }
		  }
	  }
}
